// 实现这个项目的构建任务

const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')

const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

module.exports = grunt => {

  grunt.initConfig({
    clean: ['dist', 'temp'],
    jshint: {
      lint: {
        src: ['src/assets/scripts/*.js'],
        options : {
          curly : true, //设置循环或者条件语句必须使用花括号包围
          indent : 2,
          esversion: 6,
          eqeqeq : true, //禁止使用 ==和 !=
          quotmark : 'single',
          maxlen : 120,
          asi: true
      } 
      },
    },
    csslint: {
      strict: {
        options: {
          import: 2
        },
        src: ['src/assets/styles/*.scss']
      },
      lax: {
        options: {
          import: false
        },
        src: ['src/assets/styles/*.scss']
      }
    },
    sass: {
      options: {
        implementation: sass
      },
      main: {
        files: [{
          expand: true,
          cwd: "src/",
          src: "assets/styles/*.scss",
          dest: "temp",
          ext: '.css'
        }]
      }
    },
    cssmin:{
      main:{
        files:[{
          expand: true,
          cwd: "temp/",
          src: "assets/styles/*.css",
          dest: "dist"
        }]
      }
    },
    babel: {
      options: {
        sourceMap: true,
        presets: ['@babel/preset-env']
      },
      main: {
        files: [{
          expand: true,
          cwd: "src/",
          src: "assets/scripts/*.js",
          dest: "temp"
        }]
      }
    }, 
    uglify: {
      main:{
        files:[{
          expand: true,
          cwd: "temp/",
          src: "assets/scripts/*.js",
          dest: "dist"
        }]
      }
    },
    swig: {
      main: {
        options: {
          data,
        },
        expand: true,
        cwd: 'src/',
        dest: 'temp',
        src: [
          '*.html',
          'partials/*.html',
          'layouts/*.html'
        ],
        ext: '.html'
      },
    },
    watch: {
      css: {
        files: 'src/assets/styles/*.scss',
        tasks: ['sass']
      },
      js: {
        files: 'src/assets/scripts/*.js',
        tasks: ['babel']
      },
      page: {
        files: 'src/*.html',
        tasks: ['swig']
      }
    },
    browserSync: {
      dev: {
        bsFiles: {
          src : 'temp/**'
        },
        options: {
          watchTask: true,
          notify: false,
          port: 2080,
          server: {
            baseDir: ['temp', 'src', 'public'],
            routes: {//引入文件路径替换
              '/node_modules': 'node_modules'
            }
          }
        }
      }
    },
    copy: {
      public: {
        expand: true,
        cwd: 'public/',
        src: '*',
        dest: 'dist/'
      },
    },
    htmlmin: {
      main: {
        options: {
          removeComments: true,
          collapseWhitespace: true,
          minifyCSS: true,
          minifyJS: true,
        },
        files: [{
          expand: true,
          cwd: 'temp/',
          src: [
            '*.html',
            'partials/*.html',
            'layouts/*.html'
          ],
          dest: 'dist/',
        }]
      }
    },
    imagemin: {
      /* 压缩图片大小 */
      dist: {
        options: {
          optimizationLevel: 1 //定义 PNG 图片优化水平
        },
        files: [{
          expand: true,
          cwd: 'src/',
          src: ['assets/images/*', 'assets/fonts/*'], 
          dest: 'dist'
        }]
      }
    },
    useref: {
      html: 'temp/*.html',
      temp: 'dist'
    }
  })

  loadGruntTasks(grunt)
  // grunt.registerTask('build', ['uglify', 'cssmin'])
  grunt.registerTask('lint', ['jshint', 'csslint'])
  grunt.registerTask('compile', ['clean', 'babel', 'sass', 'swig'])
  grunt.registerTask('serve', ['clean', 'compile', 'browserSync', 'watch'])
  grunt.registerTask('build', ['clean', 'compile', 'useref', 'concat', 'uglify', 'cssmin', 'htmlmin', 'copy', 'imagemin'])
}